# 一、create facelist

import requests
faceListId = "lijiahao08" #填写部分
create_facelists_url = "https://api-xls.cognitiveservices.azure.com/face/v1.0/facelists/{}" #填写
subscription_key = "85019b1c0337446294c317daeb3f435d" #填写
assert subscription_key

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscription_key,
}
data = {
  "name": "sample_list07",
  "userData": "xiangce",
  "recognitionModel": "recognition_03"# 填写
    
}

r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) #学生填写
r_create
# 返回状态码<Response [200]>

r_create.content
# 返回b''

# 二、get facelist
get_facelist_url = "https://api-xls.cognitiveservices.azure.com/face/v1.0/facelists/{}"
r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)

r_get_facelist.json()
#返回创建的列表信息
# {'persistedFaces': [],
# 'faceListId': 'lijiahao08',
# 'name': 'sample_list07',
# 'userData': 'xiangce'}

# 三、Add face
add_face_url = "https://api-xls.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces"

assert subscription_key
headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': subscription_key,
}
img_url = "http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg"

params_add_face={
    "userData":"丘某峰"
}

r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url})

r_add_face.status_code
# 返回状态码

# 检查你的facelist的信息
get_facelist_url = 
r_get_facelist = requests.get(get_facelist_url,headers=headers)
r_get_facelist.json()

# 封装成函数方便添加图片/函数——可以重复使用相同的功能
def AddFace(img_url=str,userData=str):
    add_face_url ="https://api-xls.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces"
    assert subscription_key
    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': subscription_key,
    }
    img_url = img_url

    params_add_face={
        "userData":userData
    }
    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url})
    return r_add_face.status_code#返回出状态码

# 添加图片，例如：
AddFace("http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg","丘天惠")
AddFace("http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg","林嘉茵")
AddFace("http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg","汤玲萍")
AddFace("http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg","曾雯燕")
AddFace("http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg","谢依希")
AddFace("http://huangjieqi.gitee.io/picture_storage/YuecongYang.png","杨悦聪")
AddFace("http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg","周雨")
AddFace("http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg","刘瑜鹏")
AddFace("http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg","陈嘉仪")
AddFace("http://huangjieqi.gitee.io/picture_storage/kg2000.jpg","徐旖芊")
AddFace("http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg","刘心如")
AddFace("http://huangjieqi.gitee.io/picture_storage/liuyu19.png","刘宇")
AddFace("http://huangjieqi.gitee.io/picture_storage/ltco.jpg","李婷")
AddFace("http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg","黄智毅")
AddFace("http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg","黄慧文")
AddFace("http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg","张铭睿")
AddFace("http://huangjieqi.gitee.io/picture_storage/yichenting.jpg","陈婷")
AddFace("http://huangjieqi.gitee.io/picture_storage/coco022.jpg","洪可凡")
AddFace("http://huangjieqi.gitee.io/picture_storage/lujizhi.png","卢继志")
AddFace("http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg","张梓乐")

# 检查你的facelist的信息
get_facelist_url = "https://api-xls.cognitiveservices.azure.com/face/v1.0/facelists/lijiahao08"
r_get_facelist = requests.get(get_facelist_url,headers=headers)
r_get_facelist.json()

# 四、Detect 检测人脸的id
face_api_url = 'https://api-xls.cognitiveservices.azure.com/face/v1.0/detect'

# 请求正文
image_url = 'http://huangjieqi.gitee.io/picture_storage/hjq.jpg'

headers = {'Ocp-Apim-Subscription-Key': subscription_key}

# 请求参数
params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    # 选择model
    'recognitionModel':'recognition_03',#此参数需与facelist参数一致
    'detectionModel':'detection_01',
    # 可选参数,请仔细阅读API文档
    'returnFaceAttributes': '',
}

response = requests.post(face_api_url, params=params,
                         headers=headers, json={"url": image_url})
# json.dumps 将json--->字符串
response.json()

# 返回faceId信息
#[{'faceId': '2fcb0f2d-8886-4f49-8dc0-d942b66f03fa',
# 'faceRectangle': {'top': 264, 'left': 207, 'width': 277, 'height': 277}}]

findsimilars_url = "https://api-xls.cognitiveservices.azure.com/face/v1.0/findsimilars"

# 请求正文 faceId先检测一张照片获取
data_findsimilars = {
    "faceId":"2fcb0f2d-8886-4f49-8dc0-d942b66f03fa",#取上方的faceID
    "faceListId": "lijiahao08",
#     "faceIds":faceId_02,
    "maxNumOfCandidatesReturned": 10,
    "mode": "matchFace"#matchPerson #一种为验证模式，一种为相似值模式
    }

r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)

r_findsimilars
# 返回状态码
r_findsimilars.json()
# 返回json格式的人脸信息

# 用pandas将人脸信息制表
import pandas as pd
#facelist里面的数据
faceListId_df = pd.json_normalize(r_get_facelist.json()["persistedFaces"])# 升级pandas才能运行
faceListId_df

# 返回相似度的数据
find_df = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行
find_df

# 将两组表格的信息以相同值做参考合并
pd.merge(faceListId_df, find_df,how='inner', on='persistedFaceId').sort_values(by="confidence",ascending = False)

